//
//  UIViewController+XBBaseViewController.m
//  XBFrameworkProject
//
//  Created by 徐秀滨 on 2018/11/12.
//  Copyright © 2018 徐秀滨. All rights reserved.
//

#import "UIViewController+XBBaseViewController.h"
#import <MBProgressHUD.h>
#import <objc/runtime.h>

@implementation XBViewControllerParameter

@end

@implementation UIViewController (XBBaseViewController)

#pragma mark - life

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSString *selString = @"viewDidLoad";
        NSString *mySelString = [@"xb_" stringByAppendingString:selString];
        Method originalMethod = class_getInstanceMethod(self, NSSelectorFromString(selString));
        Method myMethod = class_getInstanceMethod(self, NSSelectorFromString(mySelString));
        method_exchangeImplementations(originalMethod, myMethod);
    });
}

- (void)xb_viewDidLoad {
    if(self.parameter.needBack){
        if(self.navigationController.viewControllers.count > 0){
            self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:XBFramework_Resource(@"common_nav_back")] style:UIBarButtonItemStyleDone target:self action:@selector(goBack)];
        }
    }
    //调原先的viewDidLoad方法
    [self xb_viewDidLoad];
}

#pragma mark - Event

#pragma mark -网络请求

- (void)networkRequest:(NSString*)url
            parameters:(id)parameters
                  type:(NetworkRequestType)requestType
          successBlock:(void(^)(void))successBlock
          failureBlock:(void(^)(void))failureBlock {
    [self showHudWithHint:@""];
    [self.networkVM networkRequest:url parameters:parameters type:requestType successBlock:^{
        [self hideHud];
    } failureBlock:^{
        [self hideHud];
        [self showHudWithHint:@""];
    }];
}

#pragma mark -HUD

- (void)showHudWithHint:(NSString *)hint {
    MBProgressHUD *hud = [MBProgressHUD HUDForView:[[UIApplication sharedApplication].delegate window]];
    if(hud){
        //先隐藏
        [hud hide:NO];
    }
    hud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:hud];
    hud.labelText = hint;
    hud.mode = MBProgressHUDModeText;
    hud.color = [UIColor clearColor];
    hud.labelFont = [UIFont systemFontOfSize:15];
    [hud show:YES];
}

- (void)showHintTip:(NSString *)hint {
    if(self.isViewLoaded && self.view.window){
        //显示提示信息
        UIView *view = [[UIApplication sharedApplication].delegate window];
        MBProgressHUD *hud = [MBProgressHUD HUDForView:view];
        if (!hud) {
            hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
        }
        hud.userInteractionEnabled = NO;
        hud.labelText = hint;
        hud.mode = MBProgressHUDModeText;
        hud.xOffset = 0;
        hud.margin = 0;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:3];
    }
}

- (void)hideHud {
    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
}

#pragma mark - Action

- (void)pushViewContoller:(UIViewController*)vc animated:(BOOL)animated {
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:animated];
}

-(void)goBack {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - property

- (void)setNetworkVM:(NetworkViewModel *)networkVM {
    objc_setAssociatedObject(self, @selector(networkVM), networkVM, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NetworkViewModel*)networkVM {
    NetworkViewModel *obj = objc_getAssociatedObject(self, @selector(networkVM));
    if(!obj){
        obj = [NetworkViewModel new];
        [self setNetworkVM:obj];
    }
    return obj;
}

- (void)setParameter:(XBViewControllerParameter *)parameter {
    objc_setAssociatedObject(self, @selector(parameter), parameter, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (XBViewControllerParameter*)parameter {
    return objc_getAssociatedObject(self, @selector(parameter));
}

@end
